/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: guanli1.c
> Author:
> Mail: 
> Created Time: 2024年09月24日 星期二 00时07分32秒
***************************************************/
#include "guanli1.h"

int create_addr(void)
{
	int ret = 0;
	int sockfd = 0;
	struct sockaddr_in addr;
	memset(&addr,0,sizeof(addr));
	cJSON *json = NULL;

	addr.sin_family = AF_INET;
	addr.sin_port = htons(6868);
	addr.sin_addr.s_addr = inet_addr(ip);

	sockfd = socket(AF_INET,SOCK_STREAM,0);
	if(sockfd == -1)
	{
		perror("socket");
		exit(-1);
	}

	ret = connect(sockfd,(struct sockaddr *)&addr,sizeof(struct sockaddr));
	if(ret == -1)
	{
		perror("connect");
		exit(-1);
	}

return sockfd;
}
//查看
int exam_safety(int type,int sock)
{
char buf[1024] = {0};
char *dest = NULL;  
cJSON *json = NULL;
cJSON *json1 = NULL;
cJSON *node = NULL;
int ret = 0;
struct accounts acc;
json = cJSON_CreateObject();
cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(type));
   dest = cJSON_Print(json);
   strcpy(buf,dest);
ret = send(sock,buf,sizeof(buf),0);
if(ret == -1)
{
    perror("send");
	exit(-1);
}
 
   

while(1)
{
    //非阻塞函数
   // setnonblocking(sock);
    memset(&acc,0,sizeof(acc));
	acc.type = type;
    char buf2[1024] = {0};
    
    ret = recv(sock,buf2,sizeof(buf2),0);
    if(ret == -1)
    {
        perror("recv");
        exit(-1);
    }

     json1 = cJSON_Parse(buf2);
    node = cJSON_GetObjectItem(json1,"res");
    if(node == NULL)
    {
        printf("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(acc.res,node->valuestring); 
    }

    if(strcmp(acc.res,"break") == 0)
    {
        break;
    }

    node = cJSON_GetObjectItem(json1,"account");
    if(node == NULL)
    {
        printf("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(acc.account,node->valuestring); 
    }

    node = cJSON_GetObjectItem(json1,"password");
    if(node == NULL)
    {
        printf("can't find\n");
        exit(-1);
    }
    if(node->type == cJSON_String)
    {
        strcpy(acc.password,node->valuestring); 
    }

    printf("res:%s 账号:%s 密码:%s\n",acc.res,acc.account,acc.password);
}
cJSON_Delete(json);
}

//增加
void add_safety(int type,int sock)
{
    char buf[1024] = {0};
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;
	struct accounts acc;
    memset(&acc,0,sizeof(acc));


    	acc.type =type;
    printf("请输入新注册的账号\n");
    scanf("%s",acc.account);
    printf("请输入新注册的账号的密码\n");
    scanf("%s",acc.password);
    


    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(acc.type));
    cJSON_AddItemToObject(json,"account",cJSON_CreateString(acc.account));
    cJSON_AddItemToObject(json,"password",cJSON_CreateString(acc.password));
    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }
    type_information(sock,json);
}
//显示信息
void type_information(int sock,cJSON *json)
{
    int ret = 0;
    char buf2[30] = {0};
    ret = recv(sock,buf2,sizeof(buf2),0);
    if(ret == -1)
    {
        perror("recv");
        exit(-1);
    }
    printf("%s\n",buf2);
    cJSON_Delete(json);
}

//删除安全员
void delect_safety(int type,int sock)
{
    char buf[1024] = {0}; 
    char *dest = NULL;  
    cJSON *json = NULL;
      int ret = 0;
  	  struct accounts acc;
    memset(&acc,0,sizeof(acc));
             acc.type = type;

     printf("请输入要删除的账号\n");
    scanf("%s",acc.account);
    printf("请输入要删除的账号的密码\n");
    scanf("%s",acc.password);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(acc.type));
       cJSON_AddItemToObject(json,"account",cJSON_CreateString(acc.account));
    cJSON_AddItemToObject(json,"password",cJSON_CreateString(acc.password));
    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }

    type_information(sock,json);
}
//修改安全员
void updata_safety(int type,int sock)
{
    char buf[1024] = {0}; 
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;

      struct accounts acc;
    memset(&acc,0,sizeof(acc));
    char new_password[32] = {0};
            acc.type = type;
    printf("请输入要修改的账号\n");
    scanf("%s",acc.account);
    printf("请输入密码\n");
    scanf("%s",acc.password);
    printf("请输入修改后的密码\n");
    scanf("%s",new_password);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(acc.type));
        cJSON_AddItemToObject(json,"account",cJSON_CreateString(acc.account));
    cJSON_AddItemToObject(json,"password",cJSON_CreateString(acc.password));
    cJSON_AddItemToObject(json,"new_password",cJSON_CreateString(new_password));

    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }

    type_information(sock,json);
    
}
//获取全部气体信息信息
void all_obtain(int type, int sock)
{
    char buf[1024] = {0};
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;
    struct accounts acc;
    memset(&acc,0,sizeof(acc));
	      acc.type = type;
    json = cJSON_CreateObject();
    
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(acc.type));
    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
       
        perror("send");
		exit(-1);
    }
    pm_unpack(acc.type,sock);
}

//显示接受气体
void  pm_unpack(int type,int sock)
{
    Info da;
    int ret = 0;
    cJSON *json1 = NULL;
    cJSON *node = NULL;
    while(1)
    {
        //setnonblocking(sock);
        memset(&da,0,sizeof(da));
        char buf2[1024] = {0};
        
        ret = recv(sock,buf2,sizeof(buf2),0);
        if(ret == -1)
        {
            perror("打印：recv");
            exit(-1);
        }
      
        json1 = cJSON_Parse(buf2);
        node = cJSON_GetObjectItem(json1,"type");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
               da.type = node->valueint;
        }
        printf("type:%d\n",da.type);
        if(da.type == -1)
        {
			printf("type错误");
            break;
        } 

        node = cJSON_GetObjectItem(json1,"temp");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.temp = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"humidity");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.humidity = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"O2");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.O2 = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"CO2");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.CO2 = node->valueint;
        }

       
        node = cJSON_GetObjectItem(json1,"CH4");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.CH4 = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"CO");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_Number)
        {
            da.CO = node->valueint;
        }

        node = cJSON_GetObjectItem(json1,"time");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(da.time,node->valuestring); 
        }

        node = cJSON_GetObjectItem(json1,"pt");
        if(node == NULL)
        {
            printf("can't find\n");
            exit(-1);
        }
        if(node->type == cJSON_String)
        {
            strcpy(da.pt,node->valuestring); 
        }
        printf("type:%d 温度:%.2f 湿度:%.2f O2浓度:%.2f CO2浓度:%.2f  CH4浓度:%.2f CO浓度:%.2f 时间:%s  点位:%s ",\
        da.type,da.temp,da.humidity,da.O2,da.CO2,da.CH4,da.CO,da.time,da.pt);
    }
    cJSON_Delete(json1);
}


//非阻塞函数
void setnonblocking(int sock)
{
	int opts;
	opts = fcntl(sock,F_GETFL);
	if(opts < 0)
	{
		perror("fcntl");
		exit(-1);
	}

	opts = opts | O_NONBLOCK;
	if(fcntl(sock,F_SETFL,opts) < 0)
	{
		perror("fcntl");
		exit(-1);
	}

}
//修改管理员密码
void alter_leader(int type,int sock)
{
    char buf[1024] = {0}; 
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;
    char password[32]={0};
    char new_password[32] = {0};
    printf("请输入密码\n");
    scanf("%s",password);
    printf("请输入修改后的密码\n");
    scanf("%s",new_password);

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(type));
    cJSON_AddItemToObject(json,"password",cJSON_CreateString(password));
    cJSON_AddItemToObject(json,"new_password",cJSON_CreateString(new_password));

    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }

    type_information(sock,json);
    
}

//查询采集点


//增加采集点
   void add_type(int type,int sock)
{
  char buf[1024] = {0}; 
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;
    char new_password[32] = {0};
	Info io;
	memset(&io,0,sizeof(io));
    printf("请输入新采集点名字\n");
    scanf("%s",io.pt);
    

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(type));
    cJSON_AddItemToObject(json,"pt",cJSON_CreateString(io.pt));

    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }

    type_information(sock,json);
}
//删除采集点
   void delet_type(int type,int sock)
{
  char buf[1024] = {0}; 
    char *dest = NULL;  
    cJSON *json = NULL;
    int ret = 0;
    char new_password[32] = {0};
	Info io;
	memset(&io,0,sizeof(io));
    printf("请输入删除采集点名字\n");
    scanf("%s",io.pt);
    

    json = cJSON_CreateObject();
    cJSON_AddItemToObject(json,"type",cJSON_CreateNumber(type));
    cJSON_AddItemToObject(json,"pt",cJSON_CreateString(io.pt));

    dest = cJSON_Print(json);
    strcpy(buf,dest);

    ret = send(sock,buf,sizeof(buf),0);
    if(ret == -1)
    {
        perror("send");
    }

    type_information(sock,json);
}





















